using System.Collections.Generic;
using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class GrillItem : BaseItem
	{
		public enum State
		{
			Normal = 0,
			Burn = 1
		}

		public State state;

		private List<Transform> efcTrans;

		private List<Transform> smokeEfcs = new List<Transform>();

		private List<bool> smokeEnableds;

		private List<float> efcIntervals;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state == State.Normal)
			{
				return;
			}
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.canBurn)
				{
					childItems[i].Burn(Time.deltaTime);
				}
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool result = base.AttachItem(item, depthType, attachType, updatePosition);
			if (item.itemData.canBurn)
			{
				CheckState();
				item.StartBurn();
			}
			return result;
		}

		public override void ReleaseAttachItem(BaseItem item)
		{
			base.ReleaseAttachItem(item);
			if (state == State.Burn && item.itemData.canBurn)
			{
				CheckState();
			}
		}

		public void CheckState()
		{
			CalculateSmokeEnabled();
			bool flag = false;
			for (int i = 0; i < smokeEnableds.Count; i++)
			{
				if (smokeEnableds[i])
				{
					flag = true;
					break;
				}
			}
			if (flag)
			{
				state = State.Burn;
			}
			else
			{
				state = State.Normal;
			}
			if (state == State.Burn)
			{
				Singleton<AudioManager>.Instance.PlayAudio("sound_grilling", true, GetAudioPlayer());
				ShowSmokeEffect();
			}
			else
			{
				RemoveAudioPlayer();
				HideSmokeEffect();
			}
		}

		private void CalculateSmokeEnabled()
		{
			if (smokeEnableds == null)
			{
				smokeEnableds = new List<bool>();
				efcIntervals = new List<float>();
				int childCount = model.childCount;
				for (int i = 0; i < childCount; i++)
				{
					Transform child = model.GetChild(i);
					if (child.name.StartsWith("Efc"))
					{
						smokeEnableds.Add(false);
						efcIntervals.Add(child.localPosition.x);
					}
				}
			}
			if (smokeEnableds.Count <= 0)
			{
				return;
			}
			for (int j = 0; j < smokeEnableds.Count; j++)
			{
				smokeEnableds[j] = false;
			}
			for (int k = 0; k < childItems.Count; k++)
			{
				BaseItem baseItem = childItems[k];
				if (!baseItem.itemData.canBurn)
				{
					continue;
				}
				for (int l = 0; l < efcIntervals.Count; l++)
				{
					if (l == efcIntervals.Count - 1 || baseItem.transform.localPosition.x <= efcIntervals[l])
					{
						if (!smokeEnableds[l])
						{
							smokeEnableds[l] = true;
						}
						break;
					}
				}
			}
		}

		private void HideSmokeEffect()
		{
			for (int i = 0; i < smokeEfcs.Count; i++)
			{
				SetEffectLoop(smokeEfcs[i], false);
			}
		}

		public override void UpdateFrontDepth()
		{
			base.UpdateFrontDepth();
			if (efcTrans != null && base.itemData.scriptParams.Equals("1"))
			{
				for (int i = 0; i < efcTrans.Count; i++)
				{
					efcTrans[i].SetLocalZ(0f - depthSystem.frontThickness - 0.00018999999f);
				}
			}
		}

		private void ShowSmokeEffect()
		{
			if (string.IsNullOrEmpty(base.itemData.effect))
			{
				return;
			}
			if (efcTrans == null)
			{
				efcTrans = new List<Transform>();
				int childCount = model.childCount;
				for (int i = 0; i < childCount; i++)
				{
					Transform child = model.GetChild(i);
					if (base.itemData.scriptParams.Equals("1"))
					{
						child.SetLocalZ(0f - depthSystem.frontThickness - 0.00018999999f);
					}
					else
					{
						child.SetLocalZ(-1E-05f);
					}
					if (child.name.StartsWith("Efc"))
					{
						efcTrans.Add(child);
					}
				}
			}
			if (efcTrans.Count != smokeEfcs.Count)
			{
				for (int j = 0; j < efcTrans.Count; j++)
				{
					Transform transform = Singleton<GamePoolManager>.Instance.Spawn(PathConst.EFFECT + base.itemData.effect);
					if (transform != null)
					{
						transform.SetParent(efcTrans[j]);
						transform.localPosition = Vector3.zero;
						transform.localScale = Vector3.one;
						smokeEfcs.Add(transform);
					}
				}
			}
			UpdateSmokeEfc();
		}

		private void UpdateSmokeEfc()
		{
			for (int i = 0; i < smokeEfcs.Count; i++)
			{
				if (smokeEnableds != null && smokeEnableds.Count > 0)
				{
					if (smokeEnableds[i])
					{
						SetEffectLoop(smokeEfcs[i], true);
					}
					else
					{
						SetEffectLoop(smokeEfcs[i], false);
					}
				}
			}
		}
	}
}
